package edu.zli.lazy;

/**
 * @author ZerlindaLi create at 2021/3/18 10:34
 * @version 1.0.0
 * @description 懒汉式单例模式，在调用时才初始化实例. 但是当多线程访问时。未初始化之前，
 * 多线程同时进入getInstance()方法进行空判断，便会执行多次初始化实例。这个就破坏了单例模式。
 * 我们在条件判断里面打上断点，在LazyDriver类里面多线程操作。便可以看到
 * <code>
 * Connected to the target VM, address: '127.0.0.1:55818', transport: 'socket'
 * edu.zli.lazy.LazySingle@697cc9b5
 * edu.zli.lazy.LazySingle@44b9f14e
 * edu.zli.lazy.LazySingle@71bc1ae4
 * edu.zli.lazy.LazySingle@71bc1ae4
 * Disconnected from the target VM, address: '127.0.0.1:55818', transport: 'socket'
 *
 * Process finished with exit code 0
 * </code>
 */
public class LazySingle {

    private static LazySingle instance;

    private LazySingle(){}

    public static LazySingle getInstance() {
        if(instance == null) {

            instance = new LazySingle();
        }
        return instance;
    }
}
